<?php

/**
 * Controleur permettant de lister l'ensemble des clients
 */
require_once('dao/SimSwapDao.php');
require_once('dao/CarteSimDao.php');
if ($_SESSION['niveauAuth']=='1' or $_SESSION['niveauAuth']=='2'or $_SESSION['niveauAuth']=='3' or $_SESSION['niveauAuth']=='4'or $_SESSION['niveauAuth']=='5'or $_SESSION['niveauAuth']=='6'or $_SESSION['niveauAuth']=='7' or $_SESSION['niveauAuth']=='8'or $_SESSION['niveauAuth']=='9') {
// Initialisation des variables
$searchId = $searchName = $searchNumero = $searchSim = "";
$cartes_sim =array ();
$clients = array();


if(isset($_GET['action'])) {
			
		// Recherche par numéro 
		if (!empty($_GET['search-numero'])){
			
			$searchNumero = $_GET['search-numero'];
			$clients = SimSwapDao::searchByNumero($searchNumero);
			
		// Recherche par numéro de SIM
		} else if (!empty($_GET['search-sim'])){
			$searchSim = $_GET['search-sim'];
			$clients = SimSwapDao::searchBySim($searchSim);
		}
	// Récupération des cartes SIM disponnibles
	$cartes_sim = CarteSimDao::getCartesDisponnibles();
}
if(isset($_POST['action']) and $_POST['action']=='update')
{
	SimSwapDao::SimSwapModification($_POST['code_client_hto'],$_POST['carte_sim'],$_POST['ancienne_sim']);
	CarteSimDao::changerSimDetailClient($_POST['carte_sim'],$_POST['ndi']);
	redirect("liste-client");
}

// Rendu de la page
echo $twig->render('sim-swap.html', 
		array(	'clients' => $clients,
				'searchNumero' => $searchNumero,
				'searchSim' => $searchSim,
				'cartes_sim' => $cartes_sim,
				'message' => $message, 
				'error' => $error));
}
else {
	redirect("home-admin");
}